/**古文脚本 */
export default class TextAncient extends Laya.Script {

    private _text: string = "hello world";
    /** @prop {name:text, tips:"显示文本", type:String, default:"hello world"}*/
    public get text() {
        return this._text;
    }
    public set text(value) { 
        this._text = value;
        this.drawText();
    }
    /** @prop {name:color, tips:"字符串类型示例", type:String, default:"#0000ff"}*/
    public color: string = "#0000ff";
    /** @prop {name:layoutType, tips:"排版方式（verticalRight-0 竖排从右到左 暂未实现其它）", type:enum, default:0}*/
    public layoutType: layoutType = 0;
    /** @prop {name:animationState, tips:"动画状态-0 不开启 1 开启）", type:number, default:0}*/
    public animationState: number = 0;
    /** @prop {name:animationInterval, tips:"动画间隔单位毫秒", type:number, default:300}*/
    public animationInterval: number = 300;
    /** @prop {name:font, tips:"定义字体，比如"Arial, type:number, default:"Arial"}*/
    public font: string = "Arial";
    /** @prop {name:fontSize, tips:"定义字号，比如20", type:number, default:20}*/
    public fontSize: number = 20;

    /** @prop {name:verticalSpace, tips:"垂直方向间距", type:number, default:5}*/
    public verticalSpace: number = 5;

    /** @prop {name:horizontalSpace, tips:"水平方向间距", type:number, default:5}*/
    public horizontalSpace: number = 5;

    private sprite: Laya.Sprite;
    constructor() {
        super();
    }
    private maxWidth: number = 0;
    private maxHeight: number = 0;
    /**绘制文本 */
    drawText() {
        if (!this.sprite || !this.text)
            return; 
        this.sprite.graphics.clear();
        var j = 0;//控制行列
        var printNumber = 0;//当前输出字数
        var fonts = this.fontSize + "px" + " " + this.font;
        var xAction = (n) => { return -n * (this.fontSize + this.horizontalSpace); };
        var yAction = (n) => { return n * (this.fontSize + this.verticalSpace); };

        var playTime = 0;
        for (let i = 0; i < this.text.length; i++) {
            const str = this.text.charAt(i);

            if (str == "\\" && i < this.text.length && this.text.charAt(i + 1) == "n") {
                //判断是否应该进行断行
                // if(printNumber!=0){
                j++; printNumber = 0;//换行操作 
                //}
                i++;
                continue;
            }
            var _x = xAction(j);
            var _y = yAction(printNumber);

            if (this.animationState == 1) {
                playTime += this.animationInterval;
                Laya.timer.once(playTime, this, (el, x1, y1) => { this.drawByString(el, x1, y1, fonts); }, [str, _x, _y, fonts])
            }
            else {
                this.drawByString(str, _x, _y, fonts);
            }

            if (yAction(printNumber + 1) > this.maxHeight) {
                j++;
                printNumber = 0;
            }
            else {
                printNumber++;
            }
        }

    }
    /**绘制文本函数 */
    private drawByString(str: string, _x, _y, fonts) {
        this.sprite.graphics.fillText(str, _x, _y, fonts, this.color, "left");
    }
    initSprite() {
        if (!this.sprite) {
            this.sprite = new Laya.Sprite();
            var owners: Laya.Sprite = this.owner as Laya.Sprite;
            owners.addChild(this.sprite);
            this.maxWidth = owners.width;
            this.maxHeight = owners.height;
            this.sprite.pos(this.maxWidth - this.fontSize - this.horizontalSpace, 0);
        }
    }
    onEnable(): void {
        this.initSprite();
        this.drawText();
    }
    onDisable() {

    }
}
export enum layoutType {
    /**竖排 */
    verticalRight = 0,
}